﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services.Protocols;

namespace Doo.Extension
{
    public class VersionCacheExtension : SoapExtension
    {
        public override object GetInitializer(Type serviceType)
        {
            return null;
        }

        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
        {
            var versioncacheattr = attribute as VersionCacheAttribute;
            return versioncacheattr.VersionKey;
        }

        private string versionKey;

        public override void Initialize(object initializer)
        {
            versionKey = initializer as string;
            VersionCache.CompareRequestVersion(versionKey, true);
        }

        public override void ProcessMessage(SoapMessage message)
        {
            if (message.Stage == SoapMessageStage.BeforeSerialize)
            {
                VersionCache.SetResponseVersion(versionKey);
            }
        }
    }
}
